package com.example.smartcity_poverty.logic.util

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.Log
import android.webkit.WebView
import android.widget.ImageView
import android.widget.Toast
import com.bumptech.glide.Glide
import com.example.smartcity_poverty.App
import com.example.smartcity_poverty.App.Companion.context
import com.example.smartcity_poverty.R

/**
 *  created by ikun
 *  ANDROID STUDIO - version 4.0
 *  email: jinkunwei41@gmail.com
 * --------------------------------
 *
 **/
object Util {

    // 存储Activity
    var activityArr = ArrayList<Activity>()

    // 弹窗
    fun String.show() {
        Toast.makeText(context, this, Toast.LENGTH_SHORT).show()
    }

    // 网络图片加载
    fun glide(url: String, image: ImageView) =
        Glide.with(context)
            .load(url)
            .error(R.mipmap.ic_launcher) // 加载出错显示
            .into(image)

    // 日志
    fun lg(name: String, msg: String) {
        Log.d(name, msg)
    }


    // WebView扩展函数
    fun WebView.setUtils(webContent: String) {
        val addContentImgURL = App.addContentImgURL(webContent, App.url)
        this.loadDataWithBaseURL(null, addContentImgURL, "text/html", "utf-8", null)
        this.settings.loadWithOverviewMode = true
        this.settings.useWideViewPort = true
    }

    /**
     * 封装跳转
     */
    fun startActivityUtils(context: Context, activity: Activity) {

    }

    fun startActivityUtils(context: Context, activity: Activity, block: Intent.() -> Unit ) {
        context.startActivity(Intent(context, activity::class.java).apply {
            block
        })
    }


    /**
     * 添加Activity
     */
    fun addActivity(ac: Activity) = activityArr.add(ac)

    /**
     * 删除所有Activity
     */
    fun removeActivity() {
        for (i in activityArr) {
            if (!i.isFinishing) {
                i.finish()
            }
        }
    }


}